package com.ksc.storekeeper.client.presenter;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.TabData;
import com.gwtplatform.mvp.client.TabDataBasic;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.annotations.TabInfo;
import com.gwtplatform.mvp.client.annotations.TitleFunction;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.gwtplatform.mvp.client.proxy.TabContentProxyPlace;
import com.ksc.storekeeper.client.mvp.NameTokens;
import com.ksc.storekeeper.client.mvp.StoreKeeperGinInjector;
import com.ksc.storekeeper.client.presenter.ChangePasswordPresenter.ChangePasswordProxy;
import com.ksc.storekeeper.client.view.ChangePasswordView;
import com.ksc.storekeeper.client.view.ChangePasswordView.ChangePasswordUiHandler;
import com.ksc.storekeeper.shared.rpc.ProfileServiceAsync;

public class ChangePasswordPresenter extends Presenter<ChangePasswordView, ChangePasswordProxy> implements ChangePasswordUiHandler {

	@Inject
	private ProfileServiceAsync profileService;

	@Inject
	public ChangePasswordPresenter(EventBus eventBus, ChangePasswordView view, ChangePasswordProxy proxy) {
		super(eventBus, view, proxy);

	}

	@ProxyCodeSplit
	@NameToken(NameTokens.CHANGE_PASSWORD)
	public interface ChangePasswordProxy extends TabContentProxyPlace<ChangePasswordPresenter> {

	}

	@TabInfo(container = SettingsPresenter.class)
	static TabData getTabLabel(StoreKeeperGinInjector ginjector) {
		return new TabDataBasic(ginjector.getStoreKeeperConstants().changePassword(), 2);
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, SettingsPresenter.TYPE_SetTabContent, this);
	}

	@Override
	protected void onReveal() {
		super.onReveal();
	}

	@TitleFunction
	public String getTranslatedTitle(StoreKeeperGinInjector gInjector) {
		return gInjector.getStoreKeeperConstants().changePassword();
	}

	@Override
	public void changePassword(String old, String newPass) {
		profileService.changePassword(old, newPass, new AsyncCallback<Void>() {

			@Override
			public void onSuccess(Void result) {
				Window.alert("Change Password Successfully.");

			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Failed: " + caught.getMessage());
			}
		});

	}
}
